﻿
using System;
using UnityEditor;
using UnityEngine;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Text;
using Debug = UnityEngine.Debug;

class ExeSystemProcess
{
    public static System.Diagnostics.Process CreateShellExProcess(string cmd, string args, string workingDir = "")
    {
        var pStartInfo = new System.Diagnostics.ProcessStartInfo(cmd);
        pStartInfo.Arguments = args;
        pStartInfo.CreateNoWindow = false;
        pStartInfo.UseShellExecute = true;
        pStartInfo.RedirectStandardError = false;
        pStartInfo.RedirectStandardInput = false;
        pStartInfo.RedirectStandardOutput = false;
        if (!string.IsNullOrEmpty(workingDir))
            pStartInfo.WorkingDirectory = workingDir;
        return System.Diagnostics.Process.Start(pStartInfo);
    }

    public static void RunBat(string batfile, string args, string workingDir = "")
    {
        var p = CreateShellExProcess(batfile, args, workingDir);
        p.Close();
    }

    /// <summary>
    /// 同步执行bat
    /// </summary>
    /// <param name="batfile"></param>
    /// <param name="args"></param>
    /// <param name="workingDir"></param>
    /// <returns></returns>
    public static string RunBatSync(string batfile, string args, string workingDir = "")
    {
        var pStartInfo = new ProcessStartInfo(batfile);
        pStartInfo.Arguments = args;
        pStartInfo.CreateNoWindow = false;
        pStartInfo.UseShellExecute = false;
        pStartInfo.RedirectStandardError = true;
        pStartInfo.RedirectStandardInput = true;
        pStartInfo.RedirectStandardOutput = true;
        if (!string.IsNullOrEmpty(workingDir))
            pStartInfo.WorkingDirectory = workingDir;
        Process process = Process.Start(pStartInfo);
        string standardLog = process.StandardOutput.ReadToEnd();
        string errorLog = process.StandardError.ReadToEnd();
        if (process != null)
        {
            process.WaitForExit();
        }
        else
        {
            Debug.LogError("process == null");
        }
        return "BAT LOG===>\n"+standardLog + "\n\n ERROR======>"+errorLog;
    }

    
    public static void ProcessCommand(string command, string argument)
    {
        ProcessStartInfo start = new ProcessStartInfo(command);
        start.Arguments = argument;
        start.CreateNoWindow = false;
        start.ErrorDialog = true;
        start.UseShellExecute = true;

        if (start.UseShellExecute)
        {
            start.RedirectStandardOutput = false;
            start.RedirectStandardError = false;
            start.RedirectStandardInput = false;
        }
        else
        {
            start.RedirectStandardOutput = true;
            start.RedirectStandardError = true;
            start.RedirectStandardInput = true;
            start.StandardOutputEncoding = System.Text.UTF8Encoding.UTF8;
            start.StandardErrorEncoding = System.Text.UTF8Encoding.UTF8;
        }

        Process p = Process.Start(start);

        if (!start.UseShellExecute)
        {
            UnityEngine.Debug.Log(p.StandardOutput);
            UnityEngine.Debug.LogError(p.StandardError);
        }

        // p.WaitForExit();
         p.Close();
    }



}

